#include "shape_container.h"

ShapeContainer::ShapeContainer()
{

}

Shape* ShapeContainer::createShape(const std::string &inputData)
{
    auto it = inputData.begin();
    switch (*it) //inputData.at(0)
    {
        case 'c':
        {
            ++it; //first letter
            ++it; //single space
            int params[3];
            char tempStr[4];
            int i = 0;
            int j = 0;
            while(it != inputData.end())
            {
                if(*it != ' ')
                {
                    tempStr[j] = *it;
                    j++;
                }
                if(*it == ' ' || *it == '\n')
                {
                    params[i] = std::atoi(tempStr);
                    j = 0;
                    ++i;
                }
                ++it;
            }
            Circle *res = new Circle(params[0], params[1], params[2]);
            return res;
            break;
        }

        case 'r':
        {
            ++it; //first letter
            ++it; //single space
            int params[4];
            char tempStr[4];
            int i = 0;
            int j = 0;
            while(it != inputData.end())
            {
                if(*it != ' ')
                {
                    tempStr[j] = *it;
                    j++;
                }
                if(*it == ' ' || *it == '\n')
                {
                    params[i] = std::atoi(tempStr);
                    j = 0;
                    ++i;
                }
                ++it;
            }
            Rectangle *res = new Rectangle(params[0], params[1], params[2], params[3]);
            return res;
            break;
        }

        case 't':
        {
            ++it; //first letter
            ++it; //single space
            int params[6];
            char tempStr[4];
            int i = 0;
            int j = 0;
            while(it != inputData.end())
            {
                if(*it != ' ')
                {
                    tempStr[j] = *it;
                    j++;
                }
                if(*it == ' ' || *it == '\n')
                {
                    params[i] = std::atoi(tempStr);
                    j = 0;
                    ++i;
                }
                ++it;
            }
            Triangle *res = new Triangle(params[0], params[1], params[2], params[3], params[4], params[5]);
            return res;
            break;
        }

        default:
        {
            Shape *res = nullptr;
            return res;
            break;
        }
    }
}

Shape* ShapeContainer::take(const int &inputIndex)
{
    return storage.at(inputIndex);
}

void ShapeContainer::put(Shape *shape)
{
    storage.push_back(shape);
}

void ShapeContainer::remove(const int &inputIndex)
{
    storage.erase(storage.begin() + inputIndex);
}

void ShapeContainer::moveAll(const int &x, const int &y)
{
    Shape *temp;
    for(auto it = storage.begin(); it != storage.end(); ++it)
    {
        //advise are needed
         temp = *it;
         temp->moveShape(x, y);
    }
}

void ShapeContainer::extensionAll(const double &scale)
{
    Shape *temp;
    for(auto it = storage.begin(); it != storage.end(); ++it)
    {
        //advise are needed
         temp = *it;
         temp->extensionShape(scale);
    }
}

void ShapeContainer::toFile(const std::string &name)
{
   std::ofstream out;
   std::string data;
   Shape *temp;
   for(auto it = storage.begin(); it != storage.end(); ++it)
   {
       //advise are needed
        temp = *it;
        data += temp->saveData();
   }
   out.open(name);
   out << data;
   out.close();
}

void ShapeContainer::fromFile(const std::string &name)
{
    std::ifstream in(name);
    in.open(name);
    std::string data;
    data.assign(std::istreambuf_iterator<char>(in),
                std::istreambuf_iterator<char>()  );
    in.close();
    Shape *temp;
    for(auto it = data.begin(); it != data.end(); ++it)
    {
        std::string tempStr;
        while(*it != '\n')
        {
            tempStr += *it;
            ++it;
        }
        tempStr += *it; // + \n to string
        temp = ShapeContainer::createShape(tempStr);
        this->storage.push_back(temp);
    }
}

ShapeContainer::~ShapeContainer()
{

}

